package moutum.com.dagger2android.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;

import com.blankj.utilcode.util.ToastUtils;
import com.jakewharton.rxbinding2.view.RxView;
import com.jakewharton.rxbinding2.widget.RxTextView;

import java.security.PublicKey;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.Scheduler;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;
import moutum.com.dagger2android.R;

public class LoginActivity extends BaseActivity {

    private EditText mPhoneEt;
    private Button mGetCodeBt;
    private EditText mCodeEt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
        setMonter();
    }

    private void setMonter() {
        final int MAX_COUNT_TIME=60;
        Observable<Long> stringObservable = RxView.clicks(mGetCodeBt).throttleFirst(500,
                TimeUnit.MILLISECONDS).subscribeOn
                (AndroidSchedulers.mainThread()).flatMap(new Function<Object,
                ObservableSource<?>>() {
            @Override
            public ObservableSource<Boolean> apply(Object o) throws Exception {
                String s = mPhoneEt.getText().toString();
                if (TextUtils.isEmpty(s)) {
                    ToastUtils.showShort("phone is empty,please input");
                    return Observable.empty();
                }
                return Observable.just(true);
            }
        }).flatMap(new Function<Object, ObservableSource<Long>>() {
            @Override
            public ObservableSource<Long> apply(Object o) throws Exception {
                RxView.enabled(mGetCodeBt).accept(false);
                RxTextView.text(mGetCodeBt).accept("剩余"+MAX_COUNT_TIME+"秒");

                return Observable.interval(1,TimeUnit.SECONDS, Schedulers.io())
                        .take(MAX_COUNT_TIME)
                        .map(new Function<Long, Long>() {
                            @Override
                            public Long apply(Long aLong) throws Exception {
                                return MAX_COUNT_TIME-(aLong+1);
                            }
                        });
            }
        }).observeOn(AndroidSchedulers.mainThread());

        Consumer<Long> consumer = new Consumer<Long>() {
            @Override
            public void accept(Long s) throws Exception {
               if (s ==0)
               {
                   RxView.enabled(mGetCodeBt).accept(true);
                   RxTextView.text(mGetCodeBt).accept("发送验证码");
               }else {
                   RxTextView.text(mGetCodeBt).accept("剩余"+s+"秒");
               }
            }
        };
        Disposable subscribe = stringObservable.subscribe(consumer);

    }



    private void initView() {
        mPhoneEt = (EditText) findViewById(R.id.et_phone);
        mGetCodeBt = (Button) findViewById(R.id.bt_get_code);
        mCodeEt = (EditText) findViewById(R.id.et_code);
    }


}
